package com.homework.loans;

import io.dropwizard.Application;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

/**
 * Main application class - initializing and bootstrap, this would contain main method.
 *
 * @author Blind
 */
public class App extends Application<AppConfig> {

    private final HibernateBundle<AppConfig> hibernate = new HibernateBundle<AppConfig>(Loan.class, Customer.class, LoanExtension.class) {
        public DataSourceFactory getDataSourceFactory(AppConfig configuration) {
            return configuration.getDataSourceFactory();
        }
    };

    public void initialize(Bootstrap<AppConfig> bootstrap) {
        bootstrap.addBundle(hibernate);
    }

    public void run(AppConfig appConfig, Environment environment) throws Exception {
        final LoanDao loanDao = new LoanDao(hibernate.getSessionFactory());
        final CustomerDao customerDao = new CustomerDao(hibernate.getSessionFactory());

        environment.jersey().register(new LoanResource(loanDao, customerDao));
        environment.lifecycle().manage(new DatabaseComponent());
    }

    public static void main(String[] args) throws Exception {
        new App().run(args);
    }
}
